package com.lute.controller;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.model.Approver;
import com.lute.model.ClientAdmin;
import com.lute.model.Employee;
import com.lute.model.MailTemplate;
import com.lute.utils.SendMail;
import com.lute.utils.ServerErrorResponse;

@Controller
public class EmployeeProfileUpdateClientAdminServlet {

	@RequestMapping(value="/employeeProfileUpdateClientAdmin", method= RequestMethod.POST, headers="Content-type=application/json") 
	public @ResponseBody String employeeProfileUpdateApprover(@RequestBody String jsonFromUI, HttpServletRequest request) throws JSONException {
		String result = "";
		JSONObject jsonReq;
		JSONObject jsonRes = new JSONObject();
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}

		try {
			jsonReq = new JSONObject(jsonFromUI);
		} catch (JSONException e){
			jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			return result;
		}
		
		// withdraw data from JSON to modify employee
		String email = jsonReq.getString("email"); 
		String clientFromJson = jsonReq.getString("client");
		String businessUnitFromJson = jsonReq.getString("businessUnit");
		String employmentStatus = jsonReq.getString("employmentStatus");
		String positionFromJson = jsonReq.getString("position");
		String startWorkingFromJson = jsonReq.getString("startWorking"); 
		String workLifeLineFromJson = jsonReq.getString("workLifeLine"); 
		String billingStartDateFromJson = jsonReq.getString("billingStartDate"); 
		int employeeId = Integer.parseInt(jsonReq.getString("employeeId"));
		
		// withdraw ClientAdminId, role from session
		int sessionClientAdminId = (Integer)session.getAttribute("sessionUserId");
		String sessionClientAdminRole = (String)session.getAttribute("role");
		
		if (sessionClientAdminRole.equals("clientAdmin")) {
			if(ClientAdmin.checkPrivilege("id_user", sessionClientAdminId , employeeId)) {
				
				// MAIL UPDATE: validate + email change + send test mail
				if (SendMail.getInstance().isValidEmailAddress(email)) {
					if (Employee.updateEmail("id_user", employeeId, "employee", email)) {
						Employee employee = Employee.getEmployeeFromDB("id_user", employeeId);
						
						String toSubject = MailTemplate.NEW_EMPLOYEE_TO_EMPLOYEE_SUBJECT.toString();
						toSubject = toSubject + " " + employee.getFirst_name() + " " + employee.getLast_name();
						String toBody = MailTemplate.EMPLOYEE_CHANGED_EMAIL_ADDRESS_BODY.toString();
						
						SendMail.getInstance().sendMailTo(email,toSubject,toBody);
						
//						jsonRes.put("result", "OK");
//						result = jsonRes.toString();
					} else {
						jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
						result = jsonRes.toString();
						return result;
					}
				} else {
					jsonRes.put("result", ServerErrorResponse.INVALID_EMAIL.toString());
					result = jsonRes.toString();
					return result;
				}
				
				// startWorking
				if (Employee.updateEmployeeProfileGregorianCalendarDate("id_user", employeeId, "employee", "startWorking", startWorkingFromJson)){
//					jsonRes.put("result", "OK");
//					result = jsonRes.toString();
				} else {
					jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
					result = jsonRes.toString();
					return result;
				}

				// workLifeLine
				if (Employee.updateEmployeeProfileGregorianCalendarDate("id_user", employeeId, "employee", "workLifeLine", startWorkingFromJson)){
//					jsonRes.put("result", "OK");
//					result = jsonRes.toString();
				} else {
					jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
					result = jsonRes.toString();
					return result;
				}

				// billingStartDate
				if (Employee.updateEmployeeProfileGregorianCalendarDate("id_user", employeeId, "employee" , "billingStartDate", startWorkingFromJson)){
//					jsonRes.put("result", "OK");
//					result = jsonRes.toString();
				} else {
					jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
					result = jsonRes.toString();
					return result;
				}
				
				// client
				if (Employee.updateEmployeeProfileClient("id_user", employeeId, "employee", clientFromJson)){
//					jsonRes.put("result", "OK");
//					result = jsonRes.toString();
				} else {
					jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
					result = jsonRes.toString();
					return result;
				}
				
				// businessUnit
				if (Employee.updateEmployeeProfileBusinessUnit("id_user", employeeId, "employee", businessUnitFromJson)){
//					jsonRes.put("result", "OK");
//					result = jsonRes.toString();
				} else {
					jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
					result = jsonRes.toString();
					return result;
				}

				// employmentStatus
				if (Employee.updateEmployeeProfileEmploymentStatus("id_user", employeeId, "employee", employmentStatus)){
//					jsonRes.put("result", "OK");
//					result = jsonRes.toString();
				} else {
					jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
					result = jsonRes.toString();
					return result;
				}

				// Position
				if (Employee.updateEmployeeProfilePosition("id_user", employeeId, "employee", positionFromJson)){
//					jsonRes.put("result", "OK");
//					result = jsonRes.toString();
				} else {
					jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
					result = jsonRes.toString();
					return result;
				}
				
				jsonRes.put("result", "OK");
				result = jsonRes.toString();
				
			} else {
				jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
				result = jsonRes.toString();
				return result;
			}
		}
		return result;
	}
}
